package simulator.entities;

import java.awt.Color;
import java.awt.Graphics;

import simulator.drawengine.DrawEngine;
import simulator.enums.Direction;
import simulator.enums.LightState;
import simulator.types.Vector2f;

/**
 * perhaps better to use official 'negenoog' configuration, matches protocol
 * better (since it doesn't seem to know or care about bus traffic lights) See:
 * http://en.wikipedia.org/wiki/File:
 * Public_transportation_traffic_lights_in_NL_and_BE.svg
 */

public class BusTrafficLight extends BaseTrafficlight
{
	private static final boolean[][] lightArray_stop = {
			{ false, false, false }, { true, false, true },
			{ false, false, false } };
	private static final boolean[][] lightArray_orange = {
			{ false, false, false }, { false, true, false },
			{ false, false, false } };
	private static final boolean[][] lightArray_left = {
			{ true, false, false }, { false, false, false },
			{ false, false, true } };
	private static final boolean[][] lightArray_right = {
			{ false, false, true }, { false, false, false },
			{ true, false, false } };
	private static final boolean[][] lightArray_straight = {
			{ false, true, false }, { false, false, false },
			{ false, true, false } };
	private static final boolean[][] lightArray_all = { { true, true, true },
			{ false, false, false }, { false, true, false } };
	private static final boolean[][] lightArray_off = {
			{ false, false, false }, { false, false, false },
			{ false, false, false } };

	private boolean[][] _lights; 	//white		white		white
									//red		orange		red
									//white		white		white

	public BusTrafficLight()
	{
		_lights = lightArray_stop;
	}

	@Override
	protected boolean isValidState(LightState state)
	{
		switch (state)
		{
		case GREEN: /* translate to ALL */
		case ORANGE:
		case RED:
		case BLINK: /* translate to OFF (unsupported) */
		case BLINK_ERR: /* translate to OFF (unsupported) */
		case OFF:
		case LEFT: /*
					 * not in spec, but should be needed to conform to
					 * assignment
					 */
		case RIGHT: /*
					 * not in spec, but should be needed to conform to
					 * assignment
					 */
		case STRAIGHT: /*
						 * not in spec, but should be needed to conform to
						 * assignment
						 */
		case ALL:
			return true;
		default:
			return false;
		}
	}

	@Override
	public void setState(LightState state)
	{
		if (state == LightState.BLINK)
			super.setState(LightState.OFF);
		else if (state == LightState.BLINK_ERR)
			super.setState(LightState.OFF);
		else if (state == LightState.GREEN)
			super.setState(LightState.ALL);
		else
			super.setState(state);
	}

	@SuppressWarnings("incomplete-switch")
	@Override
	protected void updateLights()
	{
		switch (_state)
		{
		case ALL:
			_lights = lightArray_all;
			break;
		case OFF:
			_lights = lightArray_off;
			break;
		case ORANGE:
			_lights = lightArray_orange;
			break;
		case RED:
			_lights = lightArray_stop;
			break;
		case LEFT:
			_lights = lightArray_left;
			break;
		case RIGHT:
			_lights = lightArray_right;
			break;
		case STRAIGHT:
			_lights = lightArray_straight;
			break;
		}
	}

	@Override
	public void onDraw(Graphics g, Vector2f position, Direction dir)
	{
		// TODO Auto-generated method stub

		// _state status van stoplicht
		// _blinkstate wanneer stoplicht moet knipperen, geeft deze boolean aan
		// of het betreffende licht momenteel aan of uit is

		// -of-

		// _lights boolean-array welke aangeeft welke lichten aan of uit staan
		
		int gfxX = DrawEngine.getGraphicsX(position.x) - 7;
		int gfxY = DrawEngine.getGraphicsY(position.y) - 7;
		
		g.setColor(Color.DARK_GRAY);
		g.fillRect(gfxX - 7, gfxY - 7, 24, 24);
		
		
		boolean[] bools;
		boolean bool;
		for (int i=0; i<_lights.length; i++) 
		{
			bools = _lights[i];
			for (int j=0; j<bools.length; j++) 
			{
				bool = bools[j];
				if(bool)
				{
					if(i != 1)
						g.setColor(Color.white);
					else if(j != 1)
						g.setColor(Color.red);
					else
						g.setColor(Color.orange);
					g.fillOval(gfxX - 6, gfxY - 6, 7, 7);
				}
				else
				{
					g.setColor(Color.gray);
					g.fillOval(gfxX - 6, gfxY - 6, 7, 7);
				}
				gfxX += 7;
			}
			gfxX = DrawEngine.getGraphicsX(position.x) - 7;
			gfxY += 7; 
		}
	}
}
